package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.liliudong.template.biz.service.sys.SysMenuService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.request.sys.SysMenuAddRequest;
import com.liliudong.template.common.request.sys.SysMenuModifyRequest;
import com.liliudong.template.common.request.sys.SysMenuPageRequest;
import com.liliudong.template.common.response.sys.SysMenuDetailResponse;
import com.liliudong.template.common.response.sys.SysMenuPageResponse;
import com.mybatisflex.core.paginate.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 * @author liudo
 * @date 2024-08-01 08:52:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/menu")
public class SysMenuController {

	private final SysMenuService sysMenuService;

	/**
	 * 添加
	 *
	 * @param sysMenuAddRequest 新增dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:menu:add")
	@PostMapping
	public R<?> addOne(@RequestBody @Validated SysMenuAddRequest sysMenuAddRequest) {
		sysMenuService.addOne(sysMenuAddRequest);
		return R.ok();
	}

	/**
	 * 删除
	 *
	 * @param id 主键
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:menu:delete")
	@DeleteMapping("/{id}")
	public R<?> deleteById(@PathVariable Long id) {
		sysMenuService.deleteById(id);
		return R.ok();
	}

	/**
	 * 修改
	 *
	 * @param id 主键
	 * @param sysMenuModifyRequest 更新dto
	 * @return {@link R }<{@link ? }>
	 */
	@SaCheckPermission("sys:menu:modify")
	@PutMapping("/{id}")
	public R<?> modifyById(@PathVariable Long id,
	                       @RequestBody @Validated SysMenuModifyRequest sysMenuModifyRequest) {
		sysMenuService.modifyById(id, sysMenuModifyRequest);
		return R.ok();
	}

	/**
	 * 详情
	 *
	 * @param id 主键
	 * @return {@link R }<{@link SysMenuDetailResponse }>
	 */
	@SaCheckPermission("sys:menu:detail")
	@GetMapping("/detail/{id}")
	public R<SysMenuDetailResponse> detailById(@PathVariable Long id) {
		SysMenuDetailResponse sysMenuDetailVO = sysMenuService.detailById(id);
		return R.ok(sysMenuDetailVO);
	}

	/**
	 * 分页查询
	 * @param page            				 页面
	 * @param size            				 大小
	 * @param sysMenuPageRequest 分页查询条件
	 * @return {@link R }<{@link Page< SysMenuPageResponse > }>
	 */
	@SaCheckPermission("sys:menu:list")
	@GetMapping("/page/{page}/{size}")
	public R<Page<SysMenuPageResponse>> queryPage(@PathVariable Long page, @PathVariable Long size, SysMenuPageRequest sysMenuPageRequest) {
		Page<SysMenuPageResponse> pageData = sysMenuService.queryPage(page, size, sysMenuPageRequest);
		return R.ok(pageData);
	}

	/**
	 * 树状查询
	 *
	 * @return {@link R }<{@link List }<{@link Tree }<{@link Long }>>>
	 */
	@SaCheckPermission("sys:menu:list")
	@GetMapping("/tree")
	public R<List<Tree<Long>>> tree() {
		List<Tree<Long>> trees = sysMenuService.queryTree();
		return R.ok(trees);
	}
}
